﻿Imports System
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Workflow.Activities.Rules
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities
    <SRDescription("PolicyActivityDescription"), ToolboxItem(GetType(ActivityToolboxItem)), Designer(GetType(PolicyDesigner), GetType(IDesigner)), SRCategory("Standard"), ToolboxBitmap(GetType(PolicyActivity), "Resources.Rule.png")> _
    Public NotInheritable Class PolicyActivity
        Inherits Activity
        ' Methods
        Public Sub New()
        End Sub

        Public Sub New(ByVal name As String)
            MyBase.New(name)
        End Sub

        Protected Overrides Function Execute(ByVal executionContext As ActivityExecutionContext) As ActivityExecutionStatus
            If (executionContext Is Nothing) Then
                Throw New ArgumentNullException("executionContext")
            End If
            Dim declaringActivity As Activity = Helpers.GetDeclaringActivity(Me)
            If (declaringActivity Is Nothing) Then
                declaringActivity = Helpers.GetRootActivity(Me)
            End If
            Dim definitions As RuleDefinitions = DirectCast(declaringActivity.GetValue(RuleDefinitions.RuleDefinitionsProperty), RuleDefinitions)
            If (Not definitions Is Nothing) Then
                Dim set As RuleSet = definitions.RuleSets.Item(Me.RuleSetReference.RuleSetName)
                If (Not [set] Is Nothing) Then
                    [set].Execute(declaringActivity, executionContext)
                End If
            End If
            Return ActivityExecutionStatus.Closed
        End Function

        Protected Overrides Sub Initialize(ByVal provider As IServiceProvider)
            If (MyBase.Parent Is Nothing) Then
                Throw New InvalidOperationException(SR.GetString("Error_MustHaveParent"))
            End If
            MyBase.Initialize(provider)
        End Sub


        ' Properties
        <SRDescription("RuleSetDescription"), MergableProperty(False)> _
        Public Property RuleSetReference As RuleSetReference
            Get
                Return DirectCast(MyBase.GetValue(PolicyActivity.RuleSetReferenceProperty), RuleSetReference)
            End Get
            Set(ByVal value As RuleSetReference)
                MyBase.SetValue(PolicyActivity.RuleSetReferenceProperty, value)
            End Set
        End Property


        ' Fields
        Public Shared ReadOnly RuleSetReferenceProperty As DependencyProperty = DependencyProperty.Register("RuleSetReference", GetType(RuleSetReference), GetType(PolicyActivity), New PropertyMetadata(DependencyPropertyOptions.Metadata, New Attribute() { New ValidationOptionAttribute(ValidationOption.Required) }))
    End Class
End Namespace

